﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using CodeGeneratorHelper.Entities;


namespace CodeGeneratorHelper
{
    public enum LogFileType
    {
        UploadError,
        FtpUpload,
        FtpDownload,
        DownloadError,
        UpdateError,
        NoDuplicate,
        BothHFMigrated
    }
    public class UIHelper
    {
        public static void LogError(Exception error,LogFileType type)
        {
            WriteToFile("Error occured at :" + DateTime.Now, type);
            WriteToFile("Error Message :" + error.Message, type);
            WriteToFile("StackTrace :" + error.StackTrace, type);
        }
        public static string GetErrorMessage()
        {
            return "An error has occured.Please see log.txt for details or contact administrator.";
        }
        public static void WriteToFile(string str, LogFileType sType)
        {
            ChangeToCurrentDirectory();
            FileStream fs = null;
            string sFileName = GetFileName(sType);
            if (File.Exists(sFileName))
            {
                fs = new FileStream(sFileName, FileMode.Append);
            }
            else
            {
                fs = new FileStream(sFileName, FileMode.Create);
            }
            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(Environment.NewLine);
            sw.WriteLine(str);
            sw.Close();
            fs.Close();

        }
        private static string GetFileName(LogFileType sType)
        {
            string sFileName = "UploadError";
            switch (sType)
            {
                case LogFileType.UploadError:
                    sFileName = "UploadError";
                    break;
                case LogFileType.FtpUpload:
                    sFileName = "FtpUpload";
                    break;
                case LogFileType.DownloadError:
                    sFileName = "DownloadError";
                    break;
                case LogFileType.FtpDownload:
                    sFileName = "FtpDownload";
                    break;
                case LogFileType.UpdateError:
                    sFileName = "UpdateError";
                    break;
                case LogFileType.NoDuplicate:
                    sFileName = "NoDuplicate";
                    break;
                case LogFileType.BothHFMigrated:
                    sFileName = "BothMigrated";
                    break;
                
            }
            StringBuilder sbDateName = new StringBuilder();
            DateTime dtNow = DateTime.Now;

            sbDateName.Append(sFileName).Append("_").Append(dtNow.Year).Append("_").Append(dtNow.Month).Append("_").Append(dtNow.Day).Append(".txt");
            //sbDateName.Append(sFileName).Append(".txt");
            return sbDateName.ToString();
        }
        public static void ChangeToCurrentDirectory()
        {
            String path = System.Reflection.Assembly.GetExecutingAssembly().Location;
            path = System.IO.Path.GetDirectoryName(path);
            Directory.SetCurrentDirectory(path);
        }
        public static string GetStringFromByteArray(byte[] fileData)
        {
            System.Text.Encoding enc = System.Text.Encoding.ASCII;
            string str = enc.GetString(fileData);
            return str.Replace("'", "''");
        }
        public static string GetDotNetType(DBColumnType type)
        {
            string dotNetType = "string";
            switch (type)
            {
                case DBColumnType.Int:
                    dotNetType = "int";
                    break;
                case DBColumnType.DateTime:
                    dotNetType = "DateTime";
                    break;
                case DBColumnType.Date:
                    dotNetType = "DateTime";
                    break;
                case DBColumnType.Decimal:
                    dotNetType = "Decimal";
                    break;
                case DBColumnType.Numeric:
                    dotNetType = "Decimal";
                    break;
                case DBColumnType.VarChar:
                    dotNetType = "string";
                    break;
                case DBColumnType.Boolean:
                    dotNetType = "Boolean";
                    break;

            }
            return dotNetType;
        }
        public static string GetECNDotNetType(DBColumnType type)
        {
            string dotNetType = "string";
            switch (type)
            {
                case DBColumnType.Int:
                    dotNetType = "SqlInt32";
                    break;
                case DBColumnType.DateTime:
                    dotNetType = "SqlDateTime";
                    break;
                case DBColumnType.Date:
                    dotNetType = "SqlDateTime";
                    break;
                case DBColumnType.Decimal:
                    dotNetType = "SqlDecimal";
                    break;
                case DBColumnType.Numeric:
                    dotNetType = "SqlDecimal";
                    break;
                case DBColumnType.VarChar:
                    dotNetType = "string";
                    break;
                case DBColumnType.Boolean:
                    dotNetType = "SqlBoolean";
                    break;
                case DBColumnType.UniqIdentifier:
                    dotNetType = "SqlGuid";
                    break;

            }
            return dotNetType;
        }
    }
}
